package com.shf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shf.constants.SystemCanstants;
import com.shf.domain.ResponseResult;
import com.shf.domain.entity.Article;
import com.shf.domain.entity.Category;
import com.shf.domain.vo.CategoryVo;
import com.shf.domain.vo.PageVo;
import com.shf.mapper.CategoryMapper;
import com.shf.service.ArticleService;
import com.shf.service.CategoryService;
import com.shf.util.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.shf.constants.SystemCanstants.ARTICLE_STATUS_CURRENT;

/**
 * @author 叶枫
 * @date 2023/10/26.
 * ServiceImpl mybatisPlus提供的
 * 文章分类实现类
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    /**注入Article 接口层*/
    @Autowired
    private ArticleService articleService;

    /**
     * 分类下拉列表展示核心代码
     * @return
     */
    @Override
    public ResponseResult getCategoryList(){

        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        //查询出状态是已发布的文章
        queryWrapper.eq(Article::getStatus,SystemCanstants.ARTICLE_STATUS_NORMAL);
        //查询queryWrapper条件下的文章
        List<Article> articleList = articleService.list(queryWrapper);
        //对分类id进行去重 得到的就是分类id
        List<Long> categoryId = articleList.stream()
                .map(article -> article.getCategoryId())
                .distinct()
                .collect(Collectors.toList());

        //查询分类列表下status 为0也就是正常的分类名
        //根据 categoryId 集合查询 分类列表
        List<Category> categories = listByIds(categoryId);
        //筛选出 status = 0的
        List<Category> collect = categories.stream().filter(category -> SystemCanstants.STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());

        //copyBean
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(collect, CategoryVo.class);
        //响应
        return ResponseResult.okResult(categoryVos);

    }

    //----------------------------写博客-查询文章分类的接口--------------------------------------

    @Override
    public List<CategoryVo> listAllCategory() {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getStatus, SystemCanstants.NORMAL);
        List<Category> list = list(wrapper);
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(list, CategoryVo.class);
        return categoryVos;
    }

    /***
     * 分页查询分类列表
     * @param category
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageVo selectCategoryPage(Category category, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();

        queryWrapper.like(StringUtils.hasText(category.getName()),Category::getName, category.getName());
        queryWrapper.eq(Objects.nonNull(category.getStatus()),Category::getStatus, category.getStatus());

        Page<Category> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page,queryWrapper);

        //转换成VO
        List<Category> categories = page.getRecords();

        PageVo pageVo = new PageVo();
        pageVo.setTotal(page.getTotal());
        pageVo.setRows(categories);
        return pageVo;
    }


}
